package com.wannengbang.storemobile.homepage.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.bean.BatchInStorageBean;
import com.wannengbang.storemobile.utils.GlideUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 消息列表
 */
public class BatchInStorageAdapter extends RecyclerView.Adapter<BatchInStorageAdapter.ViewHolder> {

    private List<BatchInStorageBean> listsBeans;
    private Context context;
    private onCallBackListener onCallBackListener;

    public BatchInStorageAdapter(List<BatchInStorageBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    public void setOnCallBackListener(BatchInStorageAdapter.onCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_batch_in_storage_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BatchInStorageBean dataBean = listsBeans.get(position);
        if (!TextUtils.isEmpty(dataBean.getThumb())) {
            GlideUtils.loadImage(context, holder.ivTakePhoto, dataBean.getThumb());
        }
        holder.tvDelete.setOnClickListener(v -> {
            onCallBackListener.onCallBack(2, position);
        });
        holder.tvScan.setOnClickListener(v -> {
            onCallBackListener.onCallBack(3, position);
        });
        holder.editBarcode.setText(dataBean.getCode() == null ? "" : dataBean.getCode());
        holder.editName.setText(dataBean.getName() == null ? "" : dataBean.getName());
        holder.editNumber.setText(dataBean.getCount() + "");
        holder.editNumber.setSelection(String.valueOf(dataBean.getCount()).length());

    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_take_photo)
        ImageView ivTakePhoto;
        @BindView(R.id.edit_barcode)
        TextView editBarcode;
        @BindView(R.id.tv_scan)
        TextView tvScan;
        @BindView(R.id.edit_name)
        TextView editName;
        @BindView(R.id.edit_number)
        EditText editNumber;
        @BindView(R.id.tv_delete)
        TextView tvDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            editNumber.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.length() > 0) {
                        listsBeans.get(getAdapterPosition()).setCount(Integer.parseInt(s.toString()));
                    } else {
                        listsBeans.get(getAdapterPosition()).setCount(0);
                    }

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }
    }

    public interface onCallBackListener {
        void onCallBack(int type, int position);
    }
}
